package com.robot.topology.strategy.aco;

import com.robot.io.WriteInFile;
import com.robot.topology.core.GeneralAlgorithm;
import com.robot.topology.graph.domain.Graph;
import com.robot.topology.graph.service.GraphManager;



public class AntLikeII {

	public static final Integer []K = {5,10,15,50,100};
	public static final String []strategies = {"BM","INC","SHORTINC","SHORTBM"};
	private Integer nColony;
	private String filename;
	

	public AntLikeII(int colony){
		this.nColony = K[colony];
	}
	
	public AntLikeII(int colony, String filename){
		this.nColony = K[colony];
		this.filename = filename;
	}

	public void runAlgo(){
//		while convergent
//		 if first ant
//		 then 
//		 GeneralAlgorithm.deterministicAlgorithm(graphToExplore, beststrategy);
//		 else
//			 for each ant
//		 	chosenstrategy=chooseRandomlyStragegy
//			GeneralAlgorithm.deterministicAlgorithm(graphToExplore, chosenstrategy);
//			end for
//		end while
	}
	
	public void runAlgorithm(Graph graphToExplore) throws Exception{
		String fileName=null;
		String chosenStrategy;
		
		// File writer
		//Get an empty file for each run
		fileName = this.filename.isEmpty()?"AntLikeII_"+this.nColony : this.filename;
		//fileName="AntLikeI_"+this.nColony;
		
		WriteInFile writeInformation = new WriteInFile("resources", fileName,"CSV");

		//Create Manager for the graph
		GraphManager graphManager = new GraphManager(graphToExplore);

		// Create routine tables and assign to the corresponding Vertexes
		graphManager.createRoutineTables(graphToExplore);
		
		//Write in the file the RoutingTable
		if(this.filename.isEmpty())
			writeInformation.writeTextFile("Iteration;Strategy;"+graphToExplore.getLabelEdges());
		writeInformation.writeTextFile("0;--;"+graphToExplore.getValueEdges());
		
		//Explore the graph until convergence
		//Colony goes to explore graph
		int i;
		for( i = 0; i < this.nColony && !graphManager.hasConverged() ; i++){
			
			if(i == 0){
				//Choose the best strategy to explore the graph
				chosenStrategy = strategies[1];//INCREMENTAL
				GeneralAlgorithm.deterministicAlgorithmOneRobot(graphToExplore, chosenStrategy);
			}
			else{
				//Choose one of the strategies to explore the graph
				chosenStrategy = strategies[(int)(Math.random()*4)];
				GeneralAlgorithm.deterministicAlgorithmOneRobot(graphToExplore, chosenStrategy);
			}
			
			//Write in the file the RoutingTable when just run one exploration
			writeInformation.writeTextFile((i+1)+";"+chosenStrategy+";"+graphToExplore.getValueEdges());
		}
		
		//Write in the file the RoutingTable
		writeInformation.writeTextFile((i+1)+";Convergence;"+graphToExplore.getValueEdges());
		System.out.println("Colony:"+this.nColony+"  Ant:"+i);
		
	}

	
}
